"""
Author : sunshicheng
DateTime : 2021/6/4.1:29 下午
FileName : parameterize_queue_locust.py
Desc: 
"""
import queue
from locust import TaskSet, HttpUser, task, constant, LoadTestShape
import sys
import math

sys.path.append('..')
from config.base_info import Static_info as si
from config.user_token import Token

token = Token('18226287291')
headers = token.test_token()


class MyTask(TaskSet):
    @task
    def test_permission(self):
        # 取出队列中的数据
        data = self.user.queueDate.get()
        result = self.client.get(url=si.inter['saas']['saas_permission'], params=data, headers=headers,
                                 name='请求用户数据权限范围')
        print(result.text)
        # 重新塞回队列
        self.user.queueDate.put(data)


class MyUsers(HttpUser):
    # 创建一个队列
    queueDate = queue.Queue()
    account_list = ['sunshicheng']
    # 将坐席写到队列里
    for account in account_list:
        queueDate.put({"account": account})

    tasks = [MyTask]
    wait_time = constant(0.5)


# 自定义并发场景
class DoubleWave(LoadTestShape):
    min_users = 2
    peak_one_users = 60
    peak_two_users = 40
    time_limit = 60

    def tick(self):
        run_time = round(self.get_run_time())

        if run_time < self.time_limit:
            user_count = (
                    (self.peak_one_users - self.min_users)
                    * math.e ** -(((run_time / (self.time_limit / 10 * 2 / 3)) - 5) ** 2)
                    + (self.peak_two_users - self.min_users)
                    * math.e ** -(((run_time / (self.time_limit / 10 * 2 / 3)) - 10) ** 2)
                    + self.min_users
            )
            return (round(user_count), round(user_count))
        else:
            return None
